package com.ef.web.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.write.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import jxl.write.Number;

import org.apache.struts2.ServletActionContext;

import com.ef.constant.ETimeline;
import com.ef.dao.LeadsClassEntityDao;
import com.ef.util.CalendarUtil;
import com.ef.web.domain.TimeCourseCount;

public class TimeByTeacherAction extends ReportAction {

	private static final long serialVersionUID = 1L;
	
	private Date today;
	
	private ETimeline classTime;
	
	private int year;
	
	private int week;
	
	private List<TimeCourseCount> course;
	
	private List<TimeCourseCount> students;
	
	private LeadsClassEntityDao leadsClassEntityDao;
	
	public TimeByTeacherAction(){
		Calendar now = Calendar.getInstance();
		now.setFirstDayOfWeek(Calendar.MONDAY);
		int day = now.get(Calendar.DAY_OF_YEAR);
		week = now.get(Calendar.WEEK_OF_YEAR);
		year = now.get(Calendar.YEAR);
		if (day > 7 && week == 1) {
			year++;
		}
		// //NOTE week要-1
		if (year == 2010) {
			week = week - 1;
		}
		classTime=ETimeline.T0900to1000;
	}
	
	public String list(){
		int trueWeek=week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		List<Date> normal = CalendarUtil.getNormalDaysWithMonDayIsFirstDay(year,trueWeek);
		List<Date> weekend = CalendarUtil.getWeekendDaysWithMonDayIsFirstDay(year, trueWeek);
		
		List<TimeCourseCount> c = new ArrayList<TimeCourseCount>();
		List<TimeCourseCount> s = new ArrayList<TimeCourseCount>();
		
		for(Date d : normal){
			TimeCourseCount t = leadsClassEntityDao.countByTimeTeacher(d, classTime);
			c.add(t);
			TimeCourseCount x = leadsClassEntityDao.countStudentsByTime(d, classTime);
			s.add(x);
		}
		
		for(Date d : weekend){
			TimeCourseCount t = leadsClassEntityDao.countByTimeTeacher(d, classTime);
			c.add(t);
			TimeCourseCount x = leadsClassEntityDao.countStudentsByTime(d, classTime);
			s.add(x);
		}
		
		course =  c ;
		students = s ;
		
		return LIST;
	}
 
	public String export(){
		String fn="教师周统计报表["+year+"_"+week+"].xls";
		list();
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		response.setContentType("application/ms-excel;charset=UTF-8");
		try {
			response.setHeader("Content-Disposition",
		"attachment;filename="+new String(String.valueOf(fn).getBytes("gb2312"),"iso8859-1"));
		} catch (UnsupportedEncodingException e1) {
			 e1.printStackTrace();
		}
		File file = new File(ServletActionContext.getServletContext()
				.getRealPath("/temp"));
		if (!file.exists()) {
			file.mkdir();
		}
		WritableWorkbook workBook;
		try {
			workBook = Workbook.createWorkbook(new File(ServletActionContext
					.getServletContext().getRealPath("/temp/export.xls")));
			workBook.setProtected(false);
			WritableSheet wst = workBook.createSheet("export", 0);
			WritableFont font_content = new WritableFont(WritableFont.TIMES,10);
			WritableCellFormat contentStyle = new WritableCellFormat(font_content);
			contentStyle.setAlignment(Alignment.CENTRE);
			contentStyle.setBorder(Border.ALL,BorderLineStyle.THIN);
			contentStyle.setWrap(false);
			String title1 = "外教提供总课量";
			String title2 = "中教提供总课量";
			String title3 = "外教到课人数";
			String title4 = "中教到课人数";
			wst.addCell(new Label(0,0,classTime.toString(),contentStyle));
			wst.addCell(new Label(0,1,"week"+year+""+week,contentStyle));
			wst.addCell(new Label(1,1,"F2F",contentStyle));
			wst.addCell(new Label(3,1,"WK",contentStyle));
			wst.addCell(new Label(5,1,"LC",contentStyle));
			wst.mergeCells(1, 1, 2, 1);
			wst.mergeCells(3, 1, 4, 1);
			wst.mergeCells(5, 1, 6, 1);
			wst.addCell(new Label(0,2,"节数",contentStyle));
			wst.addCell(new Label(1,2,title1,contentStyle));
			wst.addCell(new Label(2,2,title2,contentStyle));
			wst.addCell(new Label(3,2,title1,contentStyle));
			wst.addCell(new Label(4,2,title2,contentStyle));
			wst.addCell(new Label(5,2,title1,contentStyle));
			wst.addCell(new Label(6,2,title2,contentStyle));
			
			for(int i = 0;i<course.size();i++){
				TimeCourseCount t = course.get(i);
				wst.addCell(new Label(0,3+i,t.getDisplayName(),contentStyle));
				wst.addCell(new Number(1,3+i,t.getFfcount(),contentStyle));
				wst.addCell(new Number(2,3+i,t.getFccount(),contentStyle));
				wst.addCell(new Number(3,3+i,t.getWfcount(),contentStyle));
				wst.addCell(new Number(4,3+i,t.getWccount(),contentStyle));
				wst.addCell(new Number(5,3+i,t.getLfcount(),contentStyle));
				wst.addCell(new Number(6,3+i,t.getLccount(),contentStyle));
			}
			
			wst.addCell(new Label(0,10,classTime.toString(),contentStyle));
			wst.addCell(new Label(0,11,"week"+year+""+week,contentStyle));
			wst.addCell(new Label(1,11,"F2F",contentStyle));
			wst.addCell(new Label(3,11,"WK",contentStyle));
			wst.addCell(new Label(5,11,"LC",contentStyle));
			
			wst.mergeCells(1, 11, 2, 11);
			wst.mergeCells(3, 11, 4, 11);
			wst.mergeCells(5, 11, 6, 11);
			
			wst.addCell(new Label(0,12,"人数",contentStyle));
			wst.addCell(new Label(1,12,title3,contentStyle));
			wst.addCell(new Label(2,12,title4,contentStyle));
			wst.addCell(new Label(3,12,title3,contentStyle));
			wst.addCell(new Label(4,12,title4,contentStyle));
			wst.addCell(new Label(5,12,title3,contentStyle));
			wst.addCell(new Label(6,12,title4,contentStyle));
			
			for(int i = 0;i<students.size();i++){
				TimeCourseCount t = students.get(i);
				wst.addCell(new Label(0,13+i,t.getDisplayName(),contentStyle));
				wst.addCell(new Number(1,13+i,t.getFfcount(),contentStyle));
				wst.addCell(new Number(2,13+i,t.getFccount(),contentStyle));
				wst.addCell(new Number(3,13+i,t.getWfcount(),contentStyle));
				wst.addCell(new Number(4,13+i,t.getWccount(),contentStyle));
				wst.addCell(new Number(5,13+i,t.getLfcount(),contentStyle));
				wst.addCell(new Number(6,13+i,t.getLccount(),contentStyle));
			}
			
			workBook.write();
			workBook.close();
			RequestDispatcher view = getRequest().getRequestDispatcher(
					"../../temp/export.xls");
			try {
				view.forward(getRequest(), getResponse());
			} catch (ServletException e) {

				e.printStackTrace();
			}

		} catch (IOException e) {

			e.printStackTrace();
		} catch (RowsExceededException e) {

			e.printStackTrace();
		} catch (WriteException e) {

			e.printStackTrace();
		}

		return NONE;
	}

	public Date getToday() {
		return today;
	}

	public void setToday(Date today) {
		this.today = today;
	}

	public String getDisplayClassTime(){
		return classTime.toString();
	}
	public String  getClassTime() {
		return classTime.name();
	}

	public void setClassTime(String classTime) {
		this.classTime =ETimeline.valueOf(classTime);
	}
	
 	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getWeek() {
		return week;
	}

	public void setWeek(int week) {
		this.week = week;
	}

	public List<TimeCourseCount> getCourse() {
		return course;
	}

	public void setCourse(List<TimeCourseCount> course) {
		this.course = course;
	}

	public List<TimeCourseCount> getStudents() {
		return students;
	}

	public void setStudents(List<TimeCourseCount> students) {
		this.students = students;
	}

	public LeadsClassEntityDao getLeadsClassEntityDao() {
		return leadsClassEntityDao;
	}

	public void setLeadsClassEntityDao(LeadsClassEntityDao leadsClassEntityDao) {
		this.leadsClassEntityDao = leadsClassEntityDao;
	}

}
